package com.jigsawapp.movies.api;

import android.os.Build;
import android.text.TextUtils;

import com.jigsawapp.movies.AppContext;
import com.jigsawapp.movies.common.CyptoUtils;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

/**
 * 封装RxVolley
 * Created by jigsaw on 2016/3/11.
 * Email: fadeblack307@163.com
 */
public class AsyncHttpHelp {
    public final static String PRIVATE_TOKEN = "private_token";
    public final static String MOVIES_PRIVATE_TOKEN = "movies@token";

    public static void get(String url, HttpCallback handler) {
        RxVolley.get(url, handler);
    }

    public static void get(String url, HttpParams httpParams, HttpCallback handler) {
        new RxVolley.Builder().url(url).params(httpParams).cacheTime(15).callback(handler).doTask();
    }

    public static void post(String url, HttpParams httpParams, HttpCallback handler) {
        RxVolley.post(url, httpParams, handler);
    }

    private static String getUserAgent() {
        AppContext appContext = AppContext.getInstance();
        StringBuilder sb = new StringBuilder("imovies@jigsaw.com");
        sb.append('/' + appContext.getPackageInfo().versionName + '_' + appContext.getPackageInfo()
                .versionCode);//APP版本
        sb.append("/Android");
        sb.append("/" + Build.VERSION.RELEASE);
        sb.append("/" + Build.MODEL);
        sb.append("/" + AppContext.getInstance().getAppId());
        return sb.toString();
    }

    public static HttpParams getPrivateTokenWithParams() {
        HttpParams params = new HttpParams();
        params.putHeaders("User-Agent", getUserAgent());
        String private_token = AppContext.getInstance().getProperty(PRIVATE_TOKEN);
        private_token = CyptoUtils.decode(MOVIES_PRIVATE_TOKEN, private_token);
        if (!TextUtils.isEmpty(private_token)) {
            params.put(PRIVATE_TOKEN, private_token);
        }
        return params;
    }

    public static HttpParams getHttpParams() {
        return getPrivateTokenWithParams();
    }
}
